package com.sgb.kjwl.view.ui.adapter.transport;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.SingleItemEntity;
import com.sgb.kjwl.utils.DefaultUtil;

import java.util.List;

/**
 * @author: 杨畔
 * @date: 2021/1/22 12:30
 * @Description: 货源自定义字段适配器
 */
public class CargoCustomValueAdapter extends BaseQuickAdapter<SingleItemEntity, BaseViewHolder> {
    //焦点下标
    private int mFocusIndex;
    //是否是key值
    private boolean mIsKey;
    private EditText key,value;

    public CargoCustomValueAdapter(@Nullable List<SingleItemEntity> data) {
        super(R.layout.item_cargo_custom_value, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, SingleItemEntity item) {
        helper.setText(R.id.et_release_cargo_key, DefaultUtil.getString(item.getLeftString()))
                .setText(R.id.et_release_cargo_value, DefaultUtil.getString(item.getRightString()))
                .addOnClickListener(R.id.iv_release_cargo_del);
        //设置文字监听
        key = helper.getView(R.id.et_release_cargo_key);
        value = helper.getView(R.id.et_release_cargo_value);
        if (item.isShowBadge()){
            helper.setVisible(R.id.iv_release_cargo_del,false);
            helper.setVisible(R.id.show_view,false);
            key.setEnabled(false);
            value.setEnabled(false);
        }
        //添加焦点监听
        key.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mIsKey = true;
                mFocusIndex = helper.getLayoutPosition();
                key.addTextChangedListener(textWatcher);
            } else {
                key.removeTextChangedListener(textWatcher);
            }
        });
        value.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mIsKey = false;
                mFocusIndex = helper.getLayoutPosition();
                value.addTextChangedListener(textWatcher);
            } else {
                value.removeTextChangedListener(textWatcher);
            }
        });
    }

    /**
     * 输入框文字输入监听
     */
    private final TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            SingleItemEntity item = mData.get(mFocusIndex);
            if (mIsKey) {
                item.setLeftString(s.toString());
            } else {
                item.setRightString(s.toString());
            }
        }
    };

    public void setHintText(String keyStr,String valueStr){
        key.setHint(keyStr);
        value.setHint(valueStr);
    }
}
